package com.itheima.controller;

import com.itheima.pojo.Article;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.ArticleService;
import com.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     *新增文章
     */
    @PostMapping
    public Result add(@RequestBody Article article) {
        String plainText = stripHtmlTags(article.getContent());
        article.setContent(plainText);
        articleService.add(article);
        return Result.success();
    }
    public String stripHtmlTags(String html) {
        // 去除所有 HTML 标签（包括 <p>、<div>、<span> 等）
        return html.replaceAll("<[^>]+>", "");
    }

    /**
     *分页查询文章
     */
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) String title
    ) {
       PageBean<Article> pb =  articleService.list(pageNum,pageSize,categoryId,state,title);
       return Result.success(pb);
    }
    /**
     * 更新文章
     */
    @PutMapping
    public Result update(@RequestBody Article article){
        int a=1;
        articleService.update(article);
        return Result.success();
    }

    /**
     * 删除文章
     */
    @DeleteMapping
    public Result delete(@RequestParam Integer id){
        articleService.deleteById(id);
        return Result.success();
    }
}
